/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyecto;

import com.proyecto.datos.EntityPlan;
import com.proyecto.modelado.Materia;
import com.proyecto.modelado.Plan;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 *
 * @author mario
 */
public class frmMantenimientoMateria extends javax.swing.JInternalFrame {

    /**
     * Creates new form frmMantenimientoMateria
     */
    
    Plan plan;
    EntityPlan entPlan;
    ArrayList arryPlan;
    int id_plan;
    Materia mate;
    
    public void CargarComboPlan(){
        
        
        arryPlan = new ArrayList();
        plan = new Plan();
        arryPlan = plan.MostrarPlan();
        JList list = new JList();
        DefaultListModel listModel = new DefaultListModel();
        listModel.addElement("[SELECCIONE]");
        if (arryPlan != null) {
            for (int i = 0; i < arryPlan.size(); i++) {
                entPlan = (EntityPlan) arryPlan.get(i);
                listModel.addElement(entPlan.getNombre_plan());
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la tabla Materia", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
        }
        list.setModel(listModel);
        this.jcmbPlan.setModel(new DefaultComboBoxModel(listModel.toArray()));
        this.jcmbPlan.setSelectedIndex(0);

    }
     
    
    public frmMantenimientoMateria() {
        initComponents();
               
        CargarComboPlan();
        this.setTitle("Nueva materia");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jcmbPlan = new javax.swing.JComboBox();
        jtxtNombre = new javax.swing.JTextField();
        jtxtCodigo = new javax.swing.JTextField();
        jbtnAgregar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();

        setClosable(true);

        jLabel1.setText("Codigo:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Plan:");

        jcmbPlan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[SELECCIONE]" }));
        jcmbPlan.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcmbPlanItemStateChanged(evt);
            }
        });

        jbtnAgregar.setText("Agregar");
        jbtnAgregar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnAgregarMouseClicked(evt);
            }
        });

        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCancelarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(73, 73, 73)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 1, Short.MAX_VALUE)
                        .addComponent(jbtnAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnCancelar))
                    .addComponent(jtxtNombre, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcmbPlan, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jtxtCodigo, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(79, 79, 79))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcmbPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAgregar)
                    .addComponent(jbtnCancelar))
                .addGap(36, 36, 36))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCancelarMouseClicked
        this.dispose();
    }//GEN-LAST:event_jbtnCancelarMouseClicked

    private void jbtnAgregarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnAgregarMouseClicked
        try{
            int id_plan = this.id_plan;
            String nombre = jtxtNombre.getText();
            String cod_asignatura = jtxtCodigo.getText();
            
            mate = new Materia();
            String msj=mate.InsertarMateria(cod_asignatura,nombre,id_plan);
            JOptionPane.showMessageDialog(rootPane, msj, "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            
            
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_jbtnAgregarMouseClicked

    private void jcmbPlanItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcmbPlanItemStateChanged
        
            if(arryPlan!=null){
                for(int i=0;i<arryPlan.size();i++){
                    entPlan=(EntityPlan)arryPlan.get(i);
                    if(jcmbPlan.getSelectedItem().equals(entPlan.getNombre_plan())){
                        this.id_plan=entPlan.getId_plan();
                    }
                }
            }
    }//GEN-LAST:event_jcmbPlanItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jbtnAgregar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JComboBox jcmbPlan;
    private javax.swing.JTextField jtxtCodigo;
    private javax.swing.JTextField jtxtNombre;
    // End of variables declaration//GEN-END:variables
}
